#include <cstdio>
#include <set>

using namespace std;

void ThreeSum1(int* data, int num)
{
	for (int i = 0; i < num - 2; ++i)
	{
		for (int j = i + 1; j < num - 1; ++j)
		{
			for (int k = j + 1; k < num; ++k)
			{
				if (data[i] + data[j] + data[k] == 0)
				{
					printf("%d+%d+%d=0\n", data[i], data[j], data[k]);
				}
			}
		}
	}
}

void ThreeSum2(int* data, int num)
{
	set<int> SetTarget;
	for (int i = 0; i < num; ++i)
	{
		SetTarget.insert(data[i]);
	}
	for (int i = 0; i < num - 2; ++i)
	{
		for (int j = i + 1; j < num - 1; ++j)
		{
			int target = 0 - data[i] - data[j];
			if (SetTarget.find(target) != SetTarget.end())
			{
				printf("%d+%d+%d=0\n", data[i], data[j], target);
			}
		}
	}
}

int pFunCmp(const void* pLeft, const void* pRight)
{
	int* pDataLeft = (int*)pLeft;
	int* pDataRight = (int*)pRight;

	return *pDataLeft - *pDataRight;
}

void ThreeSum3(int* data, int num)
{
	qsort(data, num, sizeof(int), pFunCmp);
	for (int i = 0; i < num - 2; ++i)
	{
		int left = i + 1;
		int right = num - 1;
		while(left < right)
		{
			int sum = data[i] + data[left] + data[right];
			if (sum > 0)
			{
				right--;
			}
			else if (sum < 0)
			{
				left++;
			}
			else
			{
				printf("%d+%d+%d=0\n", data[i], data[left], data[right]);
				break;
			}
		}
	}
}

int main(int argc, char **argv)
{
	int data[] = {-5, -3, -1, 0, 2, 3, 5, 7, 10 };

	ThreeSum1(data, sizeof(data)/sizeof(data[0]));
	//ThreeSum2(data, sizeof(data)/sizeof(data[0]));
	ThreeSum3(data, sizeof(data)/sizeof(data[0]));

	return 0;
}